<?php
/* @var $this GatewayController */
/* @var $model Gateway */
/* @var $form CActiveForm */ 
?> 
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=69ded0fbe1c5b68f1ddc628eb58dfbe2"></script>
<script type="text/javascript">
	$(document).ready(function(e) {
		var cityName=''; 
		var map = new BMap.Map("allmap");
		map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件 
		var myCity = new BMap.LocalCity();
		var marker = new BMap.Marker();  // 创建标注; 
		var gc = new BMap.Geocoder();  

	 	myCity.get(function (result){  
   			cityName = result.name; //根据ip得到所在城市    
	   		if($('#input_lng').val()!=""&& $('#input_lat').val()!=""){
				var point = new BMap.Point($('#input_lng').val(), $('#input_lat').val());
				map.centerAndZoom(point, 14);
				marker = new BMap.Marker(point); 
				map.addOverlay(marker); 
				marker.enableDragging();    //可拖拽
			}else{
				map.centerAndZoom(cityName,result.level);   
			}
		});   
	 	 
		$('#autoshow').bind('click',function(){
			if($("#address").val()==''){
				alert("请填写地址");
				return ;
			} 
			 gc.getPoint($("#address").val(), function(point){
			  if (point) { 
			  	 map.centerAndZoom(point, 16);
			  	 marker.setPosition(point); 
				 map.addOverlay(marker);              // 将标注添加到地图中
				 marker.enableDragging();    //可拖拽
			  	$('#input_lng').val(point.lng);
			  	$('#input_lat').val(point.lat); 
			  	$('#input_len_lat').val(point.lng,point.lat); 
			  	  gc.getLocation(point, function(rs){
			        var addComp = rs.addressComponents;
			        $("#provinces").val(addComp.province);
			        $("#citys").val(addComp.city); 
			     });        
			  }
			}, cityName); 
		});

		$('#submits').bind('click',function(){
			var p = marker.getPosition();       //获取marker的位置 
			$('#input_lng').val(p.lng);
			$('#input_lat').val(p.lat); 
			$('#input_len_lat').val(p.lng,p.lat); 
			gc.getLocation(p, function(rs){
		        var addComp = rs.addressComponents;
		        $("#provinces").val(addComp.province);
		        $("#citys").val(addComp.city); 
		     });     
		});
	}); 
</script>
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'gateway-form',
	'enableAjaxValidation'=>false,
)); ?>

	<p class="note"><span class="required">*</span> 是必填内容.</p> 

	<?php //echo $form->errorSummary($model); ?>

	<div class="row">
		
		<?php if(Yii::app()->user->name=='admin'){?>  
			<?php echo $form->labelEx($model,'companyid'); ?>
			<?php echo $form->dropDownList(
	              $model,
	              'companyid', 
	            CHtml::listData(
	                Company::model()->findAll(), 
	                'id',
	                'title'
	            )
	        ); ?> 
		
		<?php }else{
			 echo $form->hiddenField($model,'companyid',array('value'=>Yii::app()->user->id));
		}?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'gatewayname'); ?>
		<?php echo $form->textField($model,'gatewayname',array('size'=>60,'maxlength'=>128)); ?>
		<?php echo $form->error($model,'gatewayname'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'address'); ?>
		<?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>128,'id'=>"address")); ?>
		<?php echo $form->error($model,'address'); ?>
		<?php echo CHtml::button('获取经纬度',array('id'=>'autoshow'));?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'len_lat'); ?>
		<?php 
				echo $form->hiddenField($model,"len_lat",array("readonly"=>true,"id"=>"input_len_lat")) ; 
				echo $form->textField($model,"lng",array("readonly"=>true,"id"=>"input_lng")) ; 
				echo $form->textField($model,"lat",array("readonly"=>true,"id"=>"input_lat")) 
		?> 
		<?php  echo $form->error($model,'len_lat'); ?>
		<div style="clear:both;">&nbsp;</div>
		<div id="allmap"></div> 
	</div>
	<div class="row">
		 
		<?php 
		     echo $form->hiddenField($model,"provinces",array("readonly"=>true,"id"=>"provinces")) ; 
		     echo $form->hiddenField($model,"citys",array("readonly"=>true,"id"=>"citys")) ; 
		     // echo $form->textField($model,"provinces",array("readonly"=>true,"id"=>"provinces")) ; 
		     // echo $form->textField($model,"citys",array("readonly"=>true,"id"=>"citys")) ; 
		 ?> 
		 
	</div>
	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? '新增' : '修改',array("id"=>"submits",'class'=>'subButton')); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->